package view;

import controller.Controller;
import controller.MyTimer;
import controller.WinLis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import static javax.swing.JOptionPane.*;

public class EnterGameJFrame extends JFrame {
    private Controller controller;
    public EnterGameJPanel enterGameJPanel;
    public WinLis winLis;
    public JMenuBar jMenuBar;
    public JMenu jMenu;
    public JMenu helpjMenu;
    public JMenu aboutjMenu;
    public JMenuItem optionsMenuItem;
    public JMenuItem aboutMenuItem;
    public JMenuItem banbenItem;
    public JMenuItem stopItem;
    public JMenuItem soreItrm;
    public JMenuItem dealItrm;
    public JTextField userA = new JTextField();
    public JTextField userB = new JTextField();
    public JTextField userC = new JTextField();
    public JLabel scoreLabel = new JLabel("得分：0");
    public MyTimer myTimer;

    public EnterGameJFrame(Controller controller) {
        this.controller = controller;
        enterGameJPanel = new EnterGameJPanel(controller);
        winLis = new WinLis();

        setTitle("斗地主");
        setResizable(false);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        // 创建选项菜单
        jMenuBar = new JMenuBar();
        jMenu = new JMenu("选项");
        helpjMenu = new JMenu("帮助");
        aboutjMenu = new JMenu("关于");
        jMenuBar.add(jMenu);
        jMenuBar.add(helpjMenu);
        jMenuBar.add(aboutjMenu);

        // 创建选项菜单的子菜单项
        optionsMenuItem = new JMenuItem("选项(G)");
        aboutMenuItem = new JMenuItem("帮助(H)");
        banbenItem = new JMenuItem("版本(S)");
        stopItem = new JMenuItem("退出(T)");
        soreItrm = new JMenuItem("积分(Z)");
        dealItrm = new JMenuItem("发牌(P)");
        optionsMenuItem.setMnemonic(KeyEvent.VK_G); // 设置菜单项快捷键为G
        aboutMenuItem.setMnemonic(KeyEvent.VK_H); // 设置菜单项快捷键为H
        banbenItem.setMnemonic(KeyEvent.VK_S); // 设置菜单项快捷键为S
        stopItem.setMnemonic(KeyEvent.VK_T); // 设置菜单项快捷键为T
        soreItrm.setMnemonic(KeyEvent.VK_Z); // 设置菜单项快捷键为Z
        dealItrm.setMnemonic(KeyEvent.VK_P);//设置菜单单项快捷键为P
        //点击按钮重新发牌
        dealItrm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                myTimer=new MyTimer(100,controller);
            }
        });

                //添加退出
                stopItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int option = JOptionPane.showConfirmDialog(null, "正在游戏中，要退出游戏吗？", "请选择一个选项", JOptionPane.YES_NO_OPTION);
                        if (option == JOptionPane.YES_OPTION) {
                            myTimer.stop();
                            UI.gameJFrame.setVisible(true);
                            UI.enterGameJFrame.setVisible(false);
                        } else if (option == JOptionPane.NO_OPTION) {
                            // 用户选择取消退出的操作
                        }
                    }
                });

        //添加版本
        banbenItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String helpText = "斗地主版本介绍：1.9plus\n\n";

                JOptionPane.showMessageDialog(null, helpText, "斗地主规则介绍", INFORMATION_MESSAGE);
            }
        });

        // 添加菜单项点击事件监听器
        optionsMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 在点击菜单项时弹出设置框
                showSettingsDialog();
            }
        });

        aboutMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 在点击菜单项时弹出帮助对话框
                showHelpDialog();
            }
        });
        soreItrm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showJF();
            }
        });
        jMenu.add(optionsMenuItem);
        jMenu.add(stopItem);
        jMenu.add(soreItrm);
        jMenu.add(dealItrm);
        helpjMenu.add(aboutMenuItem);
        aboutjMenu.add(banbenItem);

        setJMenuBar(jMenuBar);
        add(enterGameJPanel);
        addWindowListener(winLis);


        pack();
        setLocationRelativeTo(null);
    }

    private void showSettingsDialog() {
        JDialog settingsDialog = new JDialog(this, "设置");
        settingsDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        settingsDialog.setModal(true);
        settingsDialog.setLayout(new FlowLayout());

        // 单选按钮组
        ButtonGroup difficultyGroup = new ButtonGroup();

        JRadioButton easyRadioButton = new JRadioButton("简单");
        JRadioButton mediumRadioButton = new JRadioButton("中等");
        JRadioButton hardRadioButton = new JRadioButton("困难");

        // 设置默认选中的难度
        mediumRadioButton.setSelected(true);

        difficultyGroup.add(easyRadioButton);
        difficultyGroup.add(mediumRadioButton);
        difficultyGroup.add(hardRadioButton);

        settingsDialog.add(easyRadioButton);
        settingsDialog.add(mediumRadioButton);
        settingsDialog.add(hardRadioButton);

        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取选中的难度
                String selectedDifficulty = "";
                if (easyRadioButton.isSelected()) {
                    selectedDifficulty = "简单";
                } else if (mediumRadioButton.isSelected()) {
                    selectedDifficulty = "中等";
                } else if (hardRadioButton.isSelected()) {
                    selectedDifficulty = "困难";
                }

                // 处理保存操作，使用 selectedDifficulty 变量保存选中的难度
                // ...

                settingsDialog.dispose();
            }
        });

        settingsDialog.add(saveButton);
        settingsDialog.pack();
        settingsDialog.setLocationRelativeTo(this);
        settingsDialog.setVisible(true);
    }


    private void showJF() {
        // 创建积分对话框
        JDialog scoreDialog = new JDialog(this, "积分");
        scoreDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        scoreDialog.setModal(true);
        scoreDialog.setLayout(new BorderLayout());

        // 创建积分标签
        JLabel player1ScoreLabel = new JLabel("选手1积分: 0");
        JLabel player2ScoreLabel = new JLabel("选手2积分: 0");
        JLabel player3ScoreLabel = new JLabel("选手3积分: 0");

        // 创建一个面板来放置积分标签，并使用垂直的 BoxLayout
        JPanel scorePanel = new JPanel();
        scorePanel.setLayout(new BoxLayout(scorePanel, BoxLayout.Y_AXIS));
        scorePanel.add(player1ScoreLabel);
        scorePanel.add(player2ScoreLabel);
        scorePanel.add(player3ScoreLabel);

        // 添加积分面板到对话框的中心位置
        scoreDialog.pack();
        scoreDialog.setLocationRelativeTo(this);
        scoreDialog.setVisible(true);
    }


    private void showHelpDialog() {
        String helpText = "斗地主规则介绍：\n\n" +
                "斗地主是一种扑克牌类游戏，由3个玩家进行。游戏的目标是尽可能快地出完手中的牌。\n\n" +
                "规则：\n" +
                "- 游戏开始时，一副52张扑克牌(不含大小王)会均匀地发到3个玩家手中。\n" +
                "- 地主是首先叫牌的玩家，地主的任务是压过其他两个农民玩家。\n" +
                "- 游戏中的牌有大小顺序，例如：单张、对子、三张、顺子、炸弹等。\n" +
                "- 玩家在自己的回合可以出牌，出牌必须比前一位玩家出的牌大。\n" +
                "- 若玩家没有合适的牌可以出，则可以选择不出牌，由下一位玩家继续。\n" +
                "- 游戏继续进行，直到有玩家出完所有的牌或者一方玩家先出完手中所有的牌。\n\n" +
                "祝您玩得愉快！";

        showMessageDialog(this, helpText, "斗地主规则介绍", INFORMATION_MESSAGE);
    }




}